/**
 * Created by Administrator on 2017/7/27.
 */
angular.module('module.activity_info',[])
    .config(['$stateProvider',function ($stateProvider) {
        $stateProvider
            .state('activity_info',{
                url:'/activity_info',
                params:{activityId:null,originatorId:null},
                templateUrl:'view/activity_info.html',
                controller:'activity_info_controller'
            })
    }])
    .controller("activity_info_controller",['$scope',"$stateParams","ActivityService",'OriginatorService',"ActiveEnrollService",function ($scope,$stateParams,ActivityService,OriginatorService,ActiveEnrollService) {
        $scope.active_enroll={};
        console.log("activity_audit_controller启动成功");
        console.log("$stateParams",$stateParams);
    //时间选框控件——————————————————————————————————————————————————————————————————————————————————————————————
        $('#activity_time')
            .datetimepicker({
                minView:1,
                autoclose:true,
                todayBtn:true,
                todayHighlight:true,
                initialDate:new Date(),
                format:'yyyy-mm-dd hh:ii:ss'
            })
            .on('changeDate', function(date){
                console.log(date.timeStamp);
                $scope.activity.activeTime=date.timestamp;
            });
    // ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

    //修复时间选框样式——————————————————————————————————————————————————————————————————————————————————————————————————————————————
        function repair() {
            var i_left=$(".glyphicon.icon-arrow-left");
            var i_right=$(".glyphicon.icon-arrow-right");
            for (var i in i_left){
                i_left[i].className="glyphicon glyphicon-arrow-left"
            }
            for (var i in i_right){
                i_right[i].className="glyphicon glyphicon-arrow-right";
            }
            for (i=0;i<$(".datetimepicker").length-1;i++){
                $(".datetimepicker")[i].remove();
            }
        }
        repair();
    //——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————


        function getActivity(query_terms,sfun,efun) {
            ActivityService.getActivityByActivityId(query_terms,function (data) {
                console.log(data);
                $scope.activity=data;
                if(sfun){
                    sfun()
                }
            },efun)
        }

        function getOriginator(query_terms,sfun,efun) {
            OriginatorService.getOriginatorByOriginatorId(query_terms,function (data) {
                console.log(data);
                $scope.originator=data;
                if(sfun){
                    sfun()
                }
            },efun);
        }

        $scope.get_active_enroll_list_by_state=function(state,sfun,efun){
            var query_terms={
                activeId:$stateParams.activityId,
                state:state
            };
            ActiveEnrollService.get_active_enroll_list_by_id_or_state_and_id(query_terms,
                function (data) {
                    console.log(data);
                    $scope.active_enroll_list=data;
                    if(sfun){
                        sfun()
                    }
                },efun
            );
        };

        $scope.updateActivity=function(state){
            $scope.activity.activeState=state;
            ActivityService.updateActivity($scope.activity,function (data) {
                console.log("成功",data)
            },function (data) {
                console.log('失败',data)
            })
        };

        getActivity({activeId:$stateParams.activityId},function (data) {
            getOriginator({originatorId:$scope.activity.originatorId});
        });
        $scope.get_active_enroll_list_by_state(0);

        $scope.set_active_enroll=function(active_enroll){
            $scope.active_enroll=active_enroll;
        }

        $scope.update_active_enroll=function(active_enroll,state){
            addRejectMessage(active_enroll);
            active_enroll.enrolmentState=state;
            ActiveEnrollService.update_active_enroll(active_enroll,
                function (data) {
                    console.log('成功修改活动报名信息信息',data);
                    $scope.get_active_enroll_list_by_state(state)
                },function (data) {
                    console.log('修改活动报名信息失败',data)
                    $scope.get_active_enroll_list_by_state(state)
                }
            );
        };

        var addRejectMessage=function (active_enroll) {
            active_enroll.auditMessage=rejectMessage.value
        }
    }]);